﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using PTTK_HTTT_06.BUS;
using System.Data;

namespace PTTK_HTTT_06.DAO
{
    class NGUYEN_LIEU_DB
    {
        
        public static void ThemNguyenLieu(NGUYEN_LIEU _nguyenLieu)
        {
            DataProvider _provider = new DataProvider();
            _provider.Connect();
            SqlParameter[] _para =
            {
                new SqlParameter("@TenNL",SqlDbType.NVarChar,60){Value = _nguyenLieu.TenNL},
                new SqlParameter("@SoLuongTon",SqlDbType.Int){Value = _nguyenLieu.SoLuongTon.ToString()},
                new SqlParameter("@MaDVT",SqlDbType.Int){Value = _nguyenLieu.MaDVT.ToString()},
                new SqlParameter("@LoaiNL",SqlDbType.Bit){Value = _nguyenLieu.LoaiNL.ToString()},
                new SqlParameter("@HanSuDung",SqlDbType.Int){Value = _nguyenLieu.HanSuDungToiDa.ToString()},
            };
            _provider.ExcuteProcedure("usp_ThemNguyenLieu", _para);
            _provider.Disconnect();
        }
        #region public static List<NGUYEN_LIEU> LayDanhSach()
        public static List<NGUYEN_LIEU> LayDanhSach()
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select * from NGUYEN_LIEU";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
                return null;

            List<NGUYEN_LIEU> nguyenLieuList = new List<NGUYEN_LIEU>();
            for (int i = 0, count = dt.Rows.Count; i < count; i++)
            {
                NGUYEN_LIEU nguyenLieu = new NGUYEN_LIEU();
                nguyenLieu.MaNguyenLieu = int.Parse(dt.Rows[i]["MaNguyenLieu"].ToString());
                nguyenLieu.HanSuDungToiDa = int.Parse(dt.Rows[i]["HanSuDungToiDa"].ToString());
                nguyenLieu.LoaiNguyenLieu = ((dt.Rows[i]["LoaiNguyenLieu"].ToString() == "True") ? true : false);
                nguyenLieu.MaDonViTinh = int.Parse(dt.Rows[i]["MaDonViTinh"].ToString());
                nguyenLieu.SoLuongTon = int.Parse(dt.Rows[i]["SoLuongTon"].ToString());
                nguyenLieu.TenNguyenLieu = dt.Rows[i]["TenNguyenLieu"].ToString();

                nguyenLieuList.Add(nguyenLieu);
            }

            return nguyenLieuList;
        }
        #endregion

        public static DataTable LayDSTenNguyenLieu()
        {
            DataProvider _provider = new DataProvider();
            _provider.Connect();
            string sql = "select MaNguyenLieu,TenNguyenLieu from NGUYEN_LIEU";
            return _provider.ExcuteQueryDataTable(sql);
        }
    }
}
